package com.dap.system.office.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.dap.common.core.exception.CustomException;
import com.dap.common.core.utils.DateUtils;
import com.dap.common.core.utils.SecurityUtils;
import com.dap.common.core.utils.StringUtils;
import com.dap.common.core.web.domain.BaseEntity;
import com.dap.system.office.domain.Inpot;
import com.dap.system.office.domain.TClass;
import com.dap.system.office.domain.TTeacher;
import com.dap.system.office.domain.vo.TClassSelectorVo;
import com.dap.system.office.domain.vo.TSemesterSelectorVo;
import com.dap.system.office.domain.vo.TTeacherSelectorVo;
import com.dap.system.office.mapper.TClassMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dap.system.office.mapper.TSemesterMapper;
import com.dap.system.office.domain.TSemester;
import com.dap.system.office.service.ITSemesterService;

import javax.annotation.Resource;

/**
 * 学期管理Service业务层处理
 *
 * @author dap
 * @date 2021-11-11
 */
@Service
public class TSemesterServiceImpl implements ITSemesterService
{
    @Resource
    private TSemesterMapper tSemesterMapper;

    @Resource
    private TClassMapper tClassMapper;


    /**
     * 查询学期管理
     *
     * @param id 学期管理ID
     * @return 学期管理
     */
    @Override
    public TSemester selectTSemesterById(Long id)
    {
        return tSemesterMapper.selectTSemesterById(id);
    }

    /**
     * 查询学期管理列表
     *
     * @param tSemester 学期管理
     * @return 学期管理
     */
    @Override
    public List<TSemester> selectTSemesterList(TSemester tSemester)
    {
        return tSemesterMapper.selectTSemesterList(tSemester);
    }

    /**
     * 分页查询学期管理列表
     *
     * @param tSemester 学期管理
     * @return 学期管理
     */
    @Override
    public List<TSemester> selectTSemesterListPage(TSemester tSemester)
    {
        PageHelper.startPage(tSemester.getPageNum(),tSemester.getPageSize());
        List<TSemester> list = tSemesterMapper.selectTSemesterList(tSemester);
        PageInfo<TSemester> tPageInfo = new PageInfo<>(list);
        List<TSemester> dataList = tPageInfo.getList();
        return dataList;
    }
    /**
     * 新增学期管理
     *
     * @param tSemester 学期管理
     * @return 结果
     */
    @Override
    public int insertTSemester(TSemester tSemester) {
        List<TSemester> tSemesters = tSemesterMapper.selectTSemesterList(tSemester);
        //如果没有进行插入，有则提示存在
        if (tSemesters == null || tSemesters.size() == 0) {
            tSemester.setCreateTime(DateUtils.getNowDate());
            tSemester.setDelFlag("0");
            tSemester.setCreateBy(SecurityUtils.getUsername());
            return tSemesterMapper.insertTSemester(tSemester);
        }
        return 0;
    }

    /**
     * 修改学期管理
     *
     * @param tSemester 学期管理
     * @return 结果
     */
    @Override
    public int updateTSemester(TSemester tSemester) {
        List<TSemester> tSemesters = tSemesterMapper.selectTSemesterList(tSemester);
        //如果没有进行插入，有则提示存在
        if (tSemesters == null || tSemesters.size() == 0) {
            tSemester.setUpdateTime(DateUtils.getNowDate());
            return tSemesterMapper.updateTSemester(tSemester);
        }
        throw new CustomException("该学期已存在，修改失败");
    }

    /**
     * 批量删除学期管理
     *
     * @param ids 需要删除的学期管理ID
     * @return 结果
     */
    @Override
    public int deleteTSemesterByIds(Integer[] ids)
    {

        for (Integer id: ids){
            TClass tClass = new TClass();
            tClass.setSemId(Long.parseLong(id.toString()));
            List<TClassSelectorVo> tClassSelectorVos = tClassMapper.selectTClassNameList(tClass);
            if(tClassSelectorVos.size() > 0) {
                throw new CustomException("该学期下已经存在班级，禁止删除");
            }
        }

        return tSemesterMapper.deleteTSemesterByIds(ids);
    }

    /**
     * 删除学期管理信息
     *
     * @param id 学期管理ID
     * @return 结果
     */
    @Override
    public int deleteTSemesterById(Long id)
    {
        return tSemesterMapper.deleteTSemesterById(id);
    }

    @Override
    public List<TSemesterSelectorVo> selectTSemesterNameListPage() {
        return tSemesterMapper.selectAllTTeacherNameList();
    }

    @Override
    public String importSemester(List<TSemester> semesterList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(semesterList) || semesterList.size() == 0)
        {
            throw new CustomException("导入数据不能为空！");
        }else{
            for (TSemester semesters : semesterList){
                TSemester semester=tSemesterMapper.selectTSemesterByName(semesters.getSemName());
                if (semester != null){
                    semesters.setId(semester.getId());
                    semesters.setUpdateBy(SecurityUtils.getUsername());
                    semesters.setUpdateTime(DateUtils.getNowDate());
                    tSemesterMapper.updateTSemester(semesters);
                }else{
                    semesters.setCreateTime(DateUtils.getNowDate());
                    semesters.setCreateBy(SecurityUtils.getUsername());
                    semesters.setDelFlag("0");
                    tSemesterMapper.insertTSemester(semesters);
                }
            }
            return "导入成功";
        }
    }
}
